package com.zxmr.domain.service;

import java.sql.SQLException;
import java.util.List;

import com.zxmr.CommonInit;
import com.zxmr.db.DBManager;
import com.zxmr.domain.Task;
import com.zxmr.domain.TaskState;
import com.zxmr.domain.activity.Activity;
import com.zxmr.domain.activity.ActivityEntity;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.Utils;

public class TaskService {

	DBManager db = CommonInit.getDBManager();
	
	public boolean add(Task task)
	{
		try
		{
			long id = (Long)db.getMapper().insert("task.add", task);
			task.setId(id);
			
			this.addTaskActivity(task, task.getAssigner(),ResourceManager.getInstance().getMessage("com.zxmr.utility.activity.create"));
			
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public boolean applyApprove(Task task,String response)
	{
		task.setState(TaskState.approving);
		return updateStatus(task,task.getAssignee(),ResourceManager.getInstance().getMessage("com.zxmr.utility.activity.apply",response));
	}
	public boolean reject(Task task,String response)
	{
		task.setState(TaskState.reject);
		return updateStatus(task,task.getAssigner(),ResourceManager.getInstance().getMessage("com.zxmr.utility.activity.reject",response));
	}
	public boolean approve(Task task,String response)
	{
		task.setState(TaskState.complete);
		return updateStatus(task,task.getAssigner(),ResourceManager.getInstance().getMessage("com.zxmr.utility.activity.approve",response));
	}
	private boolean updateStatus(Task task,String user,String activityContent)
	{
		try
		{	
			db.getMapper().update("task.updateState", task);
			
			addTaskActivity(task,user,activityContent);
			return true;
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	public boolean expired(Task task)
	{
		String content = ResourceManager.getInstance().getMessage("com.zxmr.utility.activity.expired");
		updateStatus(task,"system",content);
		return true;
	}
	public Task query(long id)
	{
		try
		{
			return (Task)db.getMapper().queryForObject("task.findByID", id);
		}catch(SQLException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Task> queryContractPagination(TaskSearchBean bean)
	{
		try {
			return (List<Task>)db.getMapper().queryForList("task.searchPagination", bean);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public int queryContractPaginationCount(TaskSearchBean bean)
	{
		try {
			return (Integer)db.getMapper().queryForObject("task.findCountPagination", bean);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}
	public void updateExiredTask(String endTime)
	{
		try
		{
			db.getMapper().update("task.updateExiredTask", endTime);
		}catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public void addTaskActivity(Task task,String user,String activityContent)
	{
			Activity activity = new Activity();
			activity.setEntity(ActivityEntity.task);
			activity.setEntityId(task.getId());
			activity.setTime(Utils.getCurrentTime());
			activity.setUser(user);
			activity.setContent(activityContent);
			
			
			ActivityService activityService = new ActivityService();
			activityService.addActivity(activity);
	}
}
